#include<iostream>
#include<cstring>
#include<string>
using namespace std;
const int N = 1010;
int n, m;
int f[N][N];
string s1, s2;
int main()
{
	cin >> n >> m;
	cin >> s1 >> s2;
	s1 = " " + s1;
	s2 = " " + s2;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (s1[i] == s2[j]) {
				f[i][j] = f[i - 1][j - 1] + 1;
			}
			else {
				f[i][j] = max(f[i - 1][j], f[i][j - 1]);
			}
		}
	}
	cout << f[n][m] << endl;
	return 0;
}